package com.binance.api.examples;

import static com.binance.api.client.domain.account.NewOrder.limitBuy;
import static com.binance.api.client.domain.account.NewOrder.marketBuy;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import com.binance.api.client.BinanceApiClientFactory;
import com.binance.api.client.BinanceApiRestClient;
import com.binance.api.client.domain.TimeInForce;
import com.binance.api.client.domain.account.NewOrderResponse;
import com.binance.api.client.domain.account.Order;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.account.request.OrderStatusRequest;
import com.binance.api.client.exception.BinanceApiException;

/**
 * Examples on how to place orders, cancel them, and query account information.
 */
public class MyOrdersExample {

	public static final String YOUR_API_KEY = "v3hPbAMrjrnNGP1K9T33aTx4WZ5VhQuHrT4Q46nl7vyHIbUBiFo4dSMnibJDZNd8";
	public static final String YOUR_SECRET = "HZPQ0HngFfio6vfsiOW19dtY8PoCNNK4fBIFyhbG6DsoKCikWRIVHYrv3Ehfu3DN";
	
	public static void main(String[] args) {
		try {
			System.out.println(InetAddress.getLocalHost().getHostAddress());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(YOUR_API_KEY, YOUR_SECRET);
		BinanceApiRestClient client = factory.newRestClient();

		client.getServerTime();
		System.out.println(client.getAll24HrPriceStatistics());
		// Getting list of open orders
		List<Order> openOrders = client.getOpenOrders(new OrderRequest("LINKETH"));
		System.out.println(openOrders);

		// Getting list of all orders with a limit of 10
		List<Order> allOrders = client.getAllOrders(new AllOrdersRequest("LINKETH").limit(10));
		System.out.println(allOrders);

		// Get status of a particular order
		Order order = client.getOrderStatus(new OrderStatusRequest("LINKETH", 751698L));
		System.out.println(order);
		

		// Canceling an order
		try {
			client.cancelOrder(new CancelOrderRequest("LINKETH", 756762l));
		} catch (BinanceApiException e) {
			System.out.println(e.getError().getMsg());
		}

		// Placing a test LIMIT order
		client.newOrderTest(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"));

		// Placing a test MARKET order
		client.newOrderTest(marketBuy("LINKETH", "1000"));

		// Placing a real LIMIT order
		NewOrderResponse newOrderResponse = client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"));
		System.out.println(newOrderResponse);
	}

}
